package com.lanou.test;

import java.util.*;

/**
 * @author zyj
 * @date 2022年05月19日 16:22
 */
public class StudentClass {
    //    班级名称（name），容量（capacity），学生（students），实际人数（size）。
//            * –	方法包括
// * 	构造方法，get方法，set方法，toString方法
    private String name;
    private int capacity = 50;
    private List<Student> students;
    private int size;

    public StudentClass(String name) {
        this.name = name;
        this.students = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public int size() {
        return students.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\t\t").append("班级：").append(name).append("\t")
                .append("容量：").append(capacity).append("\t")
                .append("实际人数：").append(size()).append("\n")
                .append("学号").append("\t\t")
                .append("姓名").append("\t\t")
                .append("英语").append("\t\t")
                .append("数学").append("\t\t")
                .append("计算机").append("\t\t")
                .append("总成绩").append("\n");
        if (null != students) {
            if (students.isEmpty()) {
                sb.append("班级现在还没有学生");
            } else {
                Iterator<Student> iterator = students.iterator();
                while (iterator.hasNext()) {
                    sb.append(iterator.next()).append("\n");
                }
            }
        }
        return sb.toString();
    }

    /**
     * 添加学生
     *
     * @param s
     * @return
     */
    public boolean add(Student s) {
        if (size > capacity) {
            System.out.println("班级人数已满");
            return false;
        }
        if (null == s) {
            return false;
        }
        for (Student student : students) {
            if (Objects.equals(s.getId(), student.getId())) {
                System.out.println("学生id已存在");
                return false;
            }
        }
        return students.add(s);
    }

    /**
     * 删除学生
     *
     * @param id
     * @return
     */
    public boolean delete(String id) {
        if (null == id) {
            return false;
        }
        Student s = null;
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (id.equals(student.getId())) {
                s = student;
                break;
            }
        }
        return students.remove(s);
    }

    /**
     * 通过学生的id来更新
     *
     * @param student
     * @return
     */
    public boolean update(Student student) {
        int index = getIndexById(student.getId());
        if (index == -1) {
            return false;
        }
        return students.set(index, student) != null;
    }

    public int getIndexById(String id) {
        if (null != id) {
            int index = 0;
            Iterator<Student> iterator = students.iterator();
            while (iterator.hasNext()) {
                Student s = iterator.next();
                if (Objects.equals(id, s.getId())) {
                    return index;
                }
                index++;
            }
        }
        return -1;
    }

    // 根据学生的英语成绩排升序，
    // 英语成绩一样根据数学成绩，降序，
    // 数学成绩一样按照计算机升序排列。
    public void sort() {
        students.sort((o1,o2) ->{
            if (o1.getEng() - o2.getEng() > 0){
                return 1;
            }else if (o1.getEng() - o2.getEng() < 0){
                return -1;
            }else {
                if (o1.getMath() - o2.getMath() > 0){
                    return -1;
                }else if (o1.getMath() - o2.getMath() < 0){
                    return 1;
                }else {
                    if (o1.getComp() - o2.getComp() > 0){
                        return 1;
                    }else if (o1.getComp() - o2.getComp() < 0){
                        return -1;
                    }else {
                        return 0;
                    }
                }
            }
        });
    }
}
